﻿using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using iLib.Web.UI;

namespace Cms.Web.Framework.Controls
{
    public class EditAreaMenu : Control
    {
        public string AreaID { get; set; }

        public string CancelReturnUrl { get; set; }

        public string ContentType { get; set; }

        public int DetailTemplateID { get; set; }

        public int LocaleID { get; set; }

        public int MasterTemplateID { get; set; }

        public int PageID { get; set; }

        public string PreviewMode { get; set; }

        public string SaveReturnUrl { get; set; }

        public bool TemplateHasNewerVersionThanPublished { get; set; }

        public int VersionID { get; set; }
        
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Control template = Page.LoadControl(Url.VirtualPath + "Admin/EditAreaMenuTemplate.ascx");
            while (template.Controls.Count > 0)
            {
                Control c = template.Controls[0];
                template.Controls.Remove(c);
                Controls.Add(c);
            }
            var hlEdit = (HyperLink)FindControl("hlEdit");
            var hlShare = (HyperLink)FindControl("hlShare");
            hlEdit.NavigateUrl = "javascript:void(0);";
            hlShare.NavigateUrl = "javascript:void(0);";
            switch (ContentType)
            {
                case "PageContent":
                case "ApplicationPageContent":
                    hlEdit.Attributes.Add("data-location", GetPageRedirectLocationAttribute());
                    hlEdit.Attributes.Add("onclick", GetPageRedirectScript());
                    break;
                case "MasterTemplateContent":
                    hlEdit.Attributes.Add("data-location", GetMasterTemplateRedirectLocationAttribute());
                    hlEdit.Attributes.Add("onclick", GetMasterTemplateRedirectScript());
                    break;
                case "DetailTemplateContent":
                    hlEdit.Attributes.Add("data-location", GetDetailTemplateRedirectLocationAttribute());
                    hlEdit.Attributes.Add("onclick", GetDetailTemplateRedirectScript());
                    break;
            }
            hlShare.Attributes.Add("onclick", GetShareScript());
            var hlBreakLink = (HyperLink)FindControl("hlBreakLink");
            hlBreakLink.NavigateUrl = "javascript:void(0);";
            IPageRepository pageRepository = new PageRepository();
            ISiteRepository siteRepository = new SiteRepository();
            ContentSetContainer container = null;
            if (PageID > 0)
                container = pageRepository.GetPage(PageID);
            else if (DetailTemplateID > 0)
                container = siteRepository.GetDetailTemplate(DetailTemplateID);
            else if (MasterTemplateID > 0)
                container = siteRepository.GetMasterTemplate(MasterTemplateID);
            if (container == null)
                return;
            Domain.Content content = container.GetContent(AreaID);
            if (content == null)
                return;
            if (content.GetContainers().Count > 1)
            {
                hlBreakLink.Visible = true;
                hlBreakLink.Attributes.Add("onclick", GetBreakLinkScript());
            }
        }

        private string GetPageRedirectLocationAttribute()
        {
            string attribute = Url.VirtualPath + "Admin/EditContent.aspx" +
                               "?AreaID=" + AreaID +
                               "&MasterTemplateID=" + MasterTemplateID +
                               "&LocaleID=" + LocaleID +
                               "&PageID=" + PageID;
            if (VersionID > 0)
                attribute += "&VersionID=" + VersionID;
            attribute += "&SaveReturnUrl=" + SaveReturnUrl +
                         "&CancelReturnUrl=" + CancelReturnUrl;
            return attribute;
        }

        private string GetPageRedirectScript()
        {
            return "cms.SitePreview.GetAreaWidth(this); parent.location.href = this.getAttribute('data-location');";
        }
        
        private string GetMasterTemplateRedirectLocationAttribute()
        {
            string attribute;
            if (PreviewMode == "mastertemplate" || !TemplateHasNewerVersionThanPublished)
            {
                attribute = Url.VirtualPath + "Admin/EditContent.aspx" +
                            "?AreaID=" + AreaID +
                            "&MasterTemplateID=" + MasterTemplateID +
                            "&LocaleID=" + LocaleID;
                if (VersionID > 0)
                    attribute += "&VersionID=" + VersionID;
                attribute += "&SaveReturnUrl=" + SaveReturnUrl +
                             "&CancelReturnUrl=" + CancelReturnUrl;
            }
            else
            {
                attribute = Url.VirtualPath + "Admin/EditMasterTemplate.aspx" +
                            "?ID=" + MasterTemplateID +
                            "&LocaleID=" + LocaleID + "&tab=content";
            }
            return attribute;
        }
        
        private string GetMasterTemplateRedirectScript()
        {
            if (PreviewMode == "mastertemplate")
                return "cms.SitePreview.GetAreaWidth(this); parent.location.href = this.getAttribute('data-location');";
            return
                "var __currentLink = this; parent.confirmMasterTemplateContentEditing(function (status) { if (status == 'ok') { " +
                "cms.SitePreview.GetAreaWidth(__currentLink); parent.location.href = __currentLink.getAttribute('data-location');" +
                " }});";
        }
        
        private string GetDetailTemplateRedirectLocationAttribute()
        {
            string attribute;
            if (PreviewMode == "detailtemplate" || !TemplateHasNewerVersionThanPublished)
            {
                attribute = Url.VirtualPath + "Admin/EditContent.aspx" +
                            "?AreaID=" + AreaID +
                            "&MasterTemplateID=" + MasterTemplateID +
                            "&DetailTemplateID=" + DetailTemplateID +
                            "&LocaleID=" + LocaleID;
                if (VersionID > 0)
                    attribute += "&VersionID=" + VersionID;
                attribute += "&SaveReturnUrl=" + SaveReturnUrl +
                             "&CancelReturnUrl=" + CancelReturnUrl;
            }
            else
            {
                attribute = Url.VirtualPath + "Admin/EditDetailTemplate.aspx" +
                            "?ID=" + DetailTemplateID +
                            "&LocaleID=" + LocaleID + "&tab=content";
            }
            return attribute;
        }

        private string GetDetailTemplateRedirectScript()
        {
            if (PreviewMode == "detailtemplate")
                return "cms.SitePreview.GetAreaWidth(this); parent.location.href = this.getAttribute('data-location');";
            return
                "var __currentLink = this; parent.confirmDetailTemplateContentEditing(function (status) { if (status == 'ok') { " +
                "cms.SitePreview.GetAreaWidth(__currentLink); parent.location.href = __currentLink.getAttribute('data-location');" +
                " }});";
        }

        private string GetShareScript()
        {
            string data = "Referrer=" + HttpUtility.UrlEncode(CancelReturnUrl) +
                          "&AreaID=" + HttpUtility.UrlEncode(AreaID);
            switch (ContentType)
            {
                case "PageContent":
                case "ApplicationPageContent":
                    data += "&ContainerType=page&ContainerID=" + PageID;
                    break;
                case "MasterTemplateContent":
                    data += "&ContainerType=mastertemplate&ContainerID=" + MasterTemplateID;
                    break;
                case "DetailTemplateContent":
                    data += "&ContainerType=detailtemplate&ContainerID=" + DetailTemplateID;
                    break;
            }
            return "parent.location.href='" + Url.VirtualPath + "Admin/SearchContent.aspx" +
                   "?browseitemdata=" + HttpUtility.UrlEncode(data) +
                   "';";
        }
        
        private string GetBreakLinkScript()
        {
            return "parent.breakContentLink('" + AreaID + "', '" + ContentType + "');";
        }
    }
}